<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>alpha.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_color_alpha.js.html">alpha.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities related to alpha/transparent colors and alpha color
<a name="line17"></a> * conversion.
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.color.alpha&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.color&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>/**
<a name="line26"></a> * Parses an alpha color out of a string.
<a name="line27"></a> * @param {string} str Color in some format.
<a name="line28"></a> * @return {Object} Contains two properties: &#39;hex&#39;, which is a string containing
<a name="line29"></a> *     a hex representation of the color, as well as &#39;type&#39;, which is a string
<a name="line30"></a> *     containing the type of color format passed in (&#39;hex&#39;, &#39;rgb&#39;, &#39;named&#39;).
<a name="line31"></a> */
<a name="line32"></a>goog.color.alpha.parse = function(str) {
<a name="line33"></a>  var result = {};
<a name="line34"></a>  str = String(str);
<a name="line35"></a>
<a name="line36"></a>  var maybeHex = goog.color.prependHashIfNecessaryHelper(str);
<a name="line37"></a>  if (goog.color.alpha.isValidAlphaHexColor_(maybeHex)) {
<a name="line38"></a>    result.hex = goog.color.alpha.normalizeAlphaHex_(maybeHex);
<a name="line39"></a>    result.type = &#39;hex&#39;;
<a name="line40"></a>    return result;
<a name="line41"></a>  } else {
<a name="line42"></a>    var rgba = goog.color.alpha.isValidRgbaColor_(str);
<a name="line43"></a>    if (rgba.length) {
<a name="line44"></a>      result.hex = goog.color.alpha.rgbaArrayToHex(rgba);
<a name="line45"></a>      result.type = &#39;rgba&#39;;
<a name="line46"></a>      return result;
<a name="line47"></a>    } else {
<a name="line48"></a>      var hsla = goog.color.alpha.isValidHslaColor_(str);
<a name="line49"></a>      if (hsla.length) {
<a name="line50"></a>        result.hex = goog.color.alpha.hslaArrayToHex(hsla);
<a name="line51"></a>        result.type = &#39;hsla&#39;;
<a name="line52"></a>        return result;
<a name="line53"></a>      }
<a name="line54"></a>    }
<a name="line55"></a>  }
<a name="line56"></a>  throw Error(str + &#39; is not a valid color string&#39;);
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Converts a hex representation of a color to RGBA.
<a name="line62"></a> * @param {string} hexColor Color to convert.
<a name="line63"></a> * @return {string} string of the form &#39;rgba(R,G,B,A)&#39; which can be used in
<a name="line64"></a> *    styles.
<a name="line65"></a> */
<a name="line66"></a>goog.color.alpha.hexToRgbaStyle = function(hexColor) {
<a name="line67"></a>  return goog.color.alpha.rgbaStyle_(goog.color.alpha.hexToRgba(hexColor));
<a name="line68"></a>};
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Gets the hex color part of an alpha hex color. For example, from &#39;#abcdef55&#39;
<a name="line73"></a> * return &#39;#abcdef&#39;.
<a name="line74"></a> * @param {string} colorWithAlpha The alpha hex color to get the hex color from.
<a name="line75"></a> * @return {string} The hex color where the alpha part has been stripped off.
<a name="line76"></a> */
<a name="line77"></a>goog.color.alpha.extractHexColor = function(colorWithAlpha) {
<a name="line78"></a>  if (goog.color.alpha.isValidAlphaHexColor_(colorWithAlpha)) {
<a name="line79"></a>    var fullColor = goog.color.prependHashIfNecessaryHelper(colorWithAlpha);
<a name="line80"></a>    var normalizedColor = goog.color.alpha.normalizeAlphaHex_(fullColor);
<a name="line81"></a>    return normalizedColor.substring(0, 7);
<a name="line82"></a>  } else {
<a name="line83"></a>    throw Error(colorWithAlpha + &#39; is not a valid 8-hex color string&#39;);
<a name="line84"></a>  }
<a name="line85"></a>};
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Gets the alpha color part of an alpha hex color. For example, from
<a name="line90"></a> * &#39;#abcdef55&#39; return &#39;55&#39;. The result is guaranteed to be two characters long.
<a name="line91"></a> * @param {string} colorWithAlpha The alpha hex color to get the hex color from.
<a name="line92"></a> * @return {string} The hex color where the alpha part has been stripped off.
<a name="line93"></a> */
<a name="line94"></a>goog.color.alpha.extractAlpha = function(colorWithAlpha) {
<a name="line95"></a>  if (goog.color.alpha.isValidAlphaHexColor_(colorWithAlpha)) {
<a name="line96"></a>    var fullColor = goog.color.prependHashIfNecessaryHelper(colorWithAlpha);
<a name="line97"></a>    var normalizedColor = goog.color.alpha.normalizeAlphaHex_(fullColor);
<a name="line98"></a>    return normalizedColor.substring(7, 9);
<a name="line99"></a>  } else {
<a name="line100"></a>    throw Error(colorWithAlpha + &#39; is not a valid 8-hex color string&#39;);
<a name="line101"></a>  }
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Regular expression for extracting the digits in a hex color quadruplet.
<a name="line107"></a> * @type {RegExp}
<a name="line108"></a> * @private
<a name="line109"></a> */
<a name="line110"></a>goog.color.alpha.hexQuadrupletRe_ = /#(.)(.)(.)(.)/;
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Normalize a hex representation of an alpha color.
<a name="line115"></a> * @param {string} hexColor an alpha hex color string.
<a name="line116"></a> * @return {string} hex color in the format &#39;#rrggbbaa&#39; with all lowercase
<a name="line117"></a> *     literals.
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.color.alpha.normalizeAlphaHex_ = function(hexColor) {
<a name="line121"></a>  if (!goog.color.alpha.isValidAlphaHexColor_(hexColor)) {
<a name="line122"></a>    throw Error(&quot;&#39;&quot; + hexColor + &quot;&#39; is not a valid alpha hex color&quot;);
<a name="line123"></a>  }
<a name="line124"></a>  if (hexColor.length == 5) { // of the form #RGBA
<a name="line125"></a>    hexColor = hexColor.replace(goog.color.alpha.hexQuadrupletRe_,
<a name="line126"></a>        &#39;#$1$1$2$2$3$3$4$4&#39;);
<a name="line127"></a>  }
<a name="line128"></a>  return hexColor.toLowerCase();
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Converts an 8-hex representation of a color to RGBA.
<a name="line134"></a> * @param {string} hexColor Color to convert.
<a name="line135"></a> * @return {Array} array containing [r, g, b] as ints in [0, 255].
<a name="line136"></a> */
<a name="line137"></a>goog.color.alpha.hexToRgba = function(hexColor) {
<a name="line138"></a>  // TODO(user): Enhance code sharing with goog.color, for example by
<a name="line139"></a>  //     adding a goog.color.genericHexToRgb method.
<a name="line140"></a>  hexColor = goog.color.alpha.normalizeAlphaHex_(hexColor);
<a name="line141"></a>  var r = parseInt(hexColor.substr(1, 2), 16);
<a name="line142"></a>  var g = parseInt(hexColor.substr(3, 2), 16);
<a name="line143"></a>  var b = parseInt(hexColor.substr(5, 2), 16);
<a name="line144"></a>  var a = parseInt(hexColor.substr(7, 2), 16);
<a name="line145"></a>
<a name="line146"></a>  return [r, g, b, a / 255];
<a name="line147"></a>};
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * Converts a color from RGBA to hex representation.
<a name="line152"></a> * @param {number} r Amount of red, int between 0 and 255.
<a name="line153"></a> * @param {number} g Amount of green, int between 0 and 255.
<a name="line154"></a> * @param {number} b Amount of blue, int between 0 and 255.
<a name="line155"></a> * @param {number} a Amount of alpha, float between 0 and 1.
<a name="line156"></a> * @return {string} hex representation of the color.
<a name="line157"></a> */
<a name="line158"></a>goog.color.alpha.rgbaToHex = function(r, g, b, a) {
<a name="line159"></a>  var intAlpha = Math.floor(a * 255);
<a name="line160"></a>  if (isNaN(intAlpha) || intAlpha &lt; 0 || intAlpha &gt; 255) {
<a name="line161"></a>    // TODO(user): The CSS spec says the value should be clamped.
<a name="line162"></a>    throw Error(&#39;&quot;(&#39; + r + &#39;,&#39; + g + &#39;,&#39; + b + &#39;,&#39; + a +
<a name="line163"></a>        &#39;&quot;) is not a valid RGBA color&#39;);
<a name="line164"></a>  }
<a name="line165"></a>  var hexA = goog.color.prependZeroIfNecessaryHelper(intAlpha.toString(16));
<a name="line166"></a>  return goog.color.rgbToHex(r, g, b) + hexA;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Converts a color from HSLA to hex representation.
<a name="line172"></a> * @param {number} h Amount of hue, int between 0 and 360.
<a name="line173"></a> * @param {number} s Amount of saturation, int between 0 and 100.
<a name="line174"></a> * @param {number} l Amount of lightness, int between 0 and 100.
<a name="line175"></a> * @param {number} a Amount of alpha, float between 0 and 1.
<a name="line176"></a> * @return {string} hex representation of the color.
<a name="line177"></a> */
<a name="line178"></a>goog.color.alpha.hslaToHex = function(h, s, l, a) {
<a name="line179"></a>  var intAlpha = Math.floor(a * 255);
<a name="line180"></a>  if (isNaN(intAlpha) || intAlpha &lt; 0 || intAlpha &gt; 255) {
<a name="line181"></a>    // TODO(user): The CSS spec says the value should be clamped.
<a name="line182"></a>    throw Error(&#39;&quot;(&#39; + h + &#39;,&#39; + s + &#39;,&#39; + l + &#39;,&#39; + a +
<a name="line183"></a>        &#39;&quot;) is not a valid HSLA color&#39;);
<a name="line184"></a>  }
<a name="line185"></a>  var hexA = goog.color.prependZeroIfNecessaryHelper(intAlpha.toString(16));
<a name="line186"></a>  return goog.color.hslToHex(h, s / 100, l / 100) + hexA;
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Converts a color from RGBA to hex representation.
<a name="line192"></a> * @param {Array.&lt;number&gt;} rgba Array of [r, g, b, a], with r, g, b in [0, 255]
<a name="line193"></a> *     and a in [0, 1].
<a name="line194"></a> * @return {string} hex representation of the color.
<a name="line195"></a> */
<a name="line196"></a>goog.color.alpha.rgbaArrayToHex = function(rgba) {
<a name="line197"></a>  return goog.color.alpha.rgbaToHex(rgba[0], rgba[1], rgba[2], rgba[3]);
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Converts a color from RGBA to an RGBA style string.
<a name="line203"></a> * @param {number} r Value of red, in [0, 255].
<a name="line204"></a> * @param {number} g Value of green, in [0, 255].
<a name="line205"></a> * @param {number} b Value of blue, in [0, 255].
<a name="line206"></a> * @param {number} a Value of alpha, in [0, 1].
<a name="line207"></a> * @return {string} An &#39;rgba(r,g,b,a)&#39; string ready for use in a CSS rule.
<a name="line208"></a> */
<a name="line209"></a>goog.color.alpha.rgbaToRgbaStyle = function(r, g, b, a) {
<a name="line210"></a>  if (isNaN(r) || r &lt; 0 || r &gt; 255 ||
<a name="line211"></a>      isNaN(g) || g &lt; 0 || g &gt; 255 ||
<a name="line212"></a>      isNaN(b) || b &lt; 0 || b &gt; 255 ||
<a name="line213"></a>      isNaN(a) || a &lt; 0 || a &gt; 1) {
<a name="line214"></a>    throw Error(&#39;&quot;(&#39; + r + &#39;,&#39; + g + &#39;,&#39; + b + &#39;,&#39; + a +
<a name="line215"></a>        &#39;)&quot; is not a valid RGBA color&#39;);
<a name="line216"></a>  }
<a name="line217"></a>  return goog.color.alpha.rgbaStyle_([r, g, b, a]);
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/**
<a name="line222"></a> * Converts a color from RGBA to an RGBA style string.
<a name="line223"></a> * @param {(Array.&lt;number&gt;|Float32Array)} rgba Array of [r, g, b, a],
<a name="line224"></a> *     with r, g, b in [0, 255] and a in [0, 1].
<a name="line225"></a> * @return {string} An &#39;rgba(r,g,b,a)&#39; string ready for use in a CSS rule.
<a name="line226"></a> */
<a name="line227"></a>goog.color.alpha.rgbaArrayToRgbaStyle = function(rgba) {
<a name="line228"></a>  return goog.color.alpha.rgbaToRgbaStyle(rgba[0], rgba[1], rgba[2], rgba[3]);
<a name="line229"></a>};
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>/**
<a name="line233"></a> * Converts a color from HSLA to hex representation.
<a name="line234"></a> * @param {Array.&lt;number&gt;} hsla Array of [h, s, l, a], where h is an integer in
<a name="line235"></a> *     [0, 360], s and l are integers in [0, 100], and a is in [0, 1].
<a name="line236"></a> * @return {string} hex representation of the color, such as &#39;#af457eff&#39;.
<a name="line237"></a> */
<a name="line238"></a>goog.color.alpha.hslaArrayToHex = function(hsla) {
<a name="line239"></a>  return goog.color.alpha.hslaToHex(hsla[0], hsla[1], hsla[2], hsla[3]);
<a name="line240"></a>};
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>/**
<a name="line244"></a> * Converts a color from HSLA to an RGBA style string.
<a name="line245"></a> * @param {Array.&lt;number&gt;} hsla Array of [h, s, l, a], where h is and integer in
<a name="line246"></a> *     [0, 360], s and l are integers in [0, 100], and a is in [0, 1].
<a name="line247"></a> * @return {string} An &#39;rgba(r,g,b,a)&#39; string ready for use in a CSS rule.
<a name="line248"></a> */
<a name="line249"></a>goog.color.alpha.hslaArrayToRgbaStyle = function(hsla) {
<a name="line250"></a>  return goog.color.alpha.hslaToRgbaStyle(hsla[0], hsla[1], hsla[2], hsla[3]);
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Converts a color from HSLA to an RGBA style string.
<a name="line256"></a> * @param {number} h Amount of hue, int between 0 and 360.
<a name="line257"></a> * @param {number} s Amount of saturation, int between 0 and 100.
<a name="line258"></a> * @param {number} l Amount of lightness, int between 0 and 100.
<a name="line259"></a> * @param {number} a Amount of alpha, float between 0 and 1.
<a name="line260"></a> * @return {string} An &#39;rgba(r,g,b,a)&#39; string ready for use in a CSS rule.
<a name="line261"></a> *     styles.
<a name="line262"></a> */
<a name="line263"></a>goog.color.alpha.hslaToRgbaStyle = function(h, s, l, a) {
<a name="line264"></a>  return goog.color.alpha.rgbaStyle_(goog.color.alpha.hslaToRgba(h, s, l, a));
<a name="line265"></a>};
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>/**
<a name="line269"></a> * Converts a color from HSLA color space to RGBA color space.
<a name="line270"></a> * @param {number} h Amount of hue, int between 0 and 360.
<a name="line271"></a> * @param {number} s Amount of saturation, int between 0 and 100.
<a name="line272"></a> * @param {number} l Amount of lightness, int between 0 and 100.
<a name="line273"></a> * @param {number} a Amount of alpha, float between 0 and 1.
<a name="line274"></a> * @return {Array.&lt;number&gt;} [r, g, b, a] values for the color, where r, g, b
<a name="line275"></a> *     are integers in [0, 255] and a is a float in [0, 1].
<a name="line276"></a> */
<a name="line277"></a>goog.color.alpha.hslaToRgba = function(h, s, l, a) {
<a name="line278"></a>  return goog.color.hslToRgb(h, s / 100, l / 100).concat(a);
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Converts a color from RGBA color space to HSLA color space.
<a name="line284"></a> * Modified from {@link http://en.wikipedia.org/wiki/HLS_color_space}.
<a name="line285"></a> * @param {number} r Value of red, in [0, 255].
<a name="line286"></a> * @param {number} g Value of green, in [0, 255].
<a name="line287"></a> * @param {number} b Value of blue, in [0, 255].
<a name="line288"></a> * @param {number} a Value of alpha, in [0, 255].
<a name="line289"></a> * @return {Array.&lt;number&gt;} [h, s, l, a] values for the color, with h an int in
<a name="line290"></a> *     [0, 360] and s, l and a in [0, 1].
<a name="line291"></a> */
<a name="line292"></a>goog.color.alpha.rgbaToHsla = function(r, g, b, a) {
<a name="line293"></a>  return goog.color.rgbToHsl(r, g, b).concat(a);
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * Converts a color from RGBA color space to HSLA color space.
<a name="line299"></a> * @param {Array.&lt;number&gt;} rgba [r, g, b, a] values for the color, each in
<a name="line300"></a> *     [0, 255].
<a name="line301"></a> * @return {Array.&lt;number&gt;} [h, s, l, a] values for the color, with h in
<a name="line302"></a> *     [0, 360] and s, l and a in [0, 1].
<a name="line303"></a> */
<a name="line304"></a>goog.color.alpha.rgbaArrayToHsla = function(rgba) {
<a name="line305"></a>  return goog.color.alpha.rgbaToHsla(rgba[0], rgba[1], rgba[2], rgba[3]);
<a name="line306"></a>};
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/**
<a name="line310"></a> * Helper for isValidAlphaHexColor_.
<a name="line311"></a> * @type {RegExp}
<a name="line312"></a> * @private
<a name="line313"></a> */
<a name="line314"></a>goog.color.alpha.validAlphaHexColorRe_ = /^#(?:[0-9a-f]{4}){1,2}$/i;
<a name="line315"></a>
<a name="line316"></a>
<a name="line317"></a>/**
<a name="line318"></a> * Checks if a string is a valid alpha hex color.  We expect strings of the
<a name="line319"></a> * format #RRGGBBAA (ex: #1b3d5f5b) or #RGBA (ex: #3CAF == #33CCAAFF).
<a name="line320"></a> * @param {string} str String to check.
<a name="line321"></a> * @return {boolean} Whether the string is a valid alpha hex color.
<a name="line322"></a> * @private
<a name="line323"></a> */
<a name="line324"></a>// TODO(user): Support percentages when goog.color also supports them.
<a name="line325"></a>goog.color.alpha.isValidAlphaHexColor_ = function(str) {
<a name="line326"></a>  return goog.color.alpha.validAlphaHexColorRe_.test(str);
<a name="line327"></a>};
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>/**
<a name="line331"></a> * Helper for isNormalizedAlphaHexColor_.
<a name="line332"></a> * @type {RegExp}
<a name="line333"></a> * @private
<a name="line334"></a> */
<a name="line335"></a>goog.color.alpha.normalizedAlphaHexColorRe_ = /^#[0-9a-f]{8}$/;
<a name="line336"></a>
<a name="line337"></a>
<a name="line338"></a>/**
<a name="line339"></a> * Checks if a string is a normalized alpha hex color.
<a name="line340"></a> * We expect strings of the format #RRGGBBAA (ex: #1b3d5f5b)
<a name="line341"></a> * using only lowercase letters.
<a name="line342"></a> * @param {string} str String to check.
<a name="line343"></a> * @return {boolean} Whether the string is a normalized hex color.
<a name="line344"></a> * @private
<a name="line345"></a> */
<a name="line346"></a>goog.color.alpha.isNormalizedAlphaHexColor_ = function(str) {
<a name="line347"></a>  return goog.color.alpha.normalizedAlphaHexColorRe_.test(str);
<a name="line348"></a>};
<a name="line349"></a>
<a name="line350"></a>
<a name="line351"></a>/**
<a name="line352"></a> * Regular expression for matching and capturing RGBA style strings. Helper for
<a name="line353"></a> * isValidRgbaColor_.
<a name="line354"></a> * @type {RegExp}
<a name="line355"></a> * @private
<a name="line356"></a> */
<a name="line357"></a>goog.color.alpha.rgbaColorRe_ =
<a name="line358"></a>    /^(?:rgba)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|1|0\.\d{0,10})\)$/i;
<a name="line359"></a>
<a name="line360"></a>
<a name="line361"></a>/**
<a name="line362"></a> * Regular expression for matching and capturing HSLA style strings. Helper for
<a name="line363"></a> * isValidHslaColor_.
<a name="line364"></a> * @type {RegExp}
<a name="line365"></a> * @private
<a name="line366"></a> */
<a name="line367"></a>goog.color.alpha.hslaColorRe_ =
<a name="line368"></a>    /^(?:hsla)\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2})\%,\s?(0|[1-9]\d{0,2})\%,\s?(0|1|0\.\d{0,10})\)$/i;
<a name="line369"></a>
<a name="line370"></a>
<a name="line371"></a>/**
<a name="line372"></a> * Checks if a string is a valid rgba color.  We expect strings of the format
<a name="line373"></a> * &#39;(r, g, b, a)&#39;, or &#39;rgba(r, g, b, a)&#39;, where r, g, b are ints in [0, 255]
<a name="line374"></a> *     and a is a float in [0, 1].
<a name="line375"></a> * @param {string} str String to check.
<a name="line376"></a> * @return {Array.&lt;number&gt;} the integers [r, g, b, a] for valid colors or the
<a name="line377"></a> *     empty array for invalid colors.
<a name="line378"></a> * @private
<a name="line379"></a> */
<a name="line380"></a>goog.color.alpha.isValidRgbaColor_ = function(str) {
<a name="line381"></a>  // Each component is separate (rather than using a repeater) so we can
<a name="line382"></a>  // capture the match. Also, we explicitly set each component to be either 0,
<a name="line383"></a>  // or start with a non-zero, to prevent octal numbers from slipping through.
<a name="line384"></a>  var regExpResultArray = str.match(goog.color.alpha.rgbaColorRe_);
<a name="line385"></a>  if (regExpResultArray) {
<a name="line386"></a>    var r = Number(regExpResultArray[1]);
<a name="line387"></a>    var g = Number(regExpResultArray[2]);
<a name="line388"></a>    var b = Number(regExpResultArray[3]);
<a name="line389"></a>    var a = Number(regExpResultArray[4]);
<a name="line390"></a>    if (r &gt;= 0 &amp;&amp; r &lt;= 255 &amp;&amp;
<a name="line391"></a>        g &gt;= 0 &amp;&amp; g &lt;= 255 &amp;&amp;
<a name="line392"></a>        b &gt;= 0 &amp;&amp; b &lt;= 255 &amp;&amp;
<a name="line393"></a>        a &gt;= 0 &amp;&amp; a &lt;= 1) {
<a name="line394"></a>      return [r, g, b, a];
<a name="line395"></a>    }
<a name="line396"></a>  }
<a name="line397"></a>  return [];
<a name="line398"></a>};
<a name="line399"></a>
<a name="line400"></a>
<a name="line401"></a>/**
<a name="line402"></a> * Checks if a string is a valid hsla color.  We expect strings of the format
<a name="line403"></a> * &#39;hsla(h, s, l, a)&#39;, where s in an int in [0, 360], s and l are percentages
<a name="line404"></a> *     between 0 and 100 such as &#39;50%&#39; or &#39;70%&#39;, and a is a float in [0, 1].
<a name="line405"></a> * @param {string} str String to check.
<a name="line406"></a> * @return {Array.&lt;number&gt;} the integers [h, s, l, a] for valid colors or the
<a name="line407"></a> *     empty array for invalid colors.
<a name="line408"></a> * @private
<a name="line409"></a> */
<a name="line410"></a>goog.color.alpha.isValidHslaColor_ = function(str) {
<a name="line411"></a>  // Each component is separate (rather than using a repeater) so we can
<a name="line412"></a>  // capture the match. Also, we explicitly set each component to be either 0,
<a name="line413"></a>  // or start with a non-zero, to prevent octal numbers from slipping through.
<a name="line414"></a>  var regExpResultArray = str.match(goog.color.alpha.hslaColorRe_);
<a name="line415"></a>  if (regExpResultArray) {
<a name="line416"></a>    var h = Number(regExpResultArray[1]);
<a name="line417"></a>    var s = Number(regExpResultArray[2]);
<a name="line418"></a>    var l = Number(regExpResultArray[3]);
<a name="line419"></a>    var a = Number(regExpResultArray[4]);
<a name="line420"></a>    if (h &gt;= 0 &amp;&amp; h &lt;= 360 &amp;&amp;
<a name="line421"></a>        s &gt;= 0 &amp;&amp; s &lt;= 100 &amp;&amp;
<a name="line422"></a>        l &gt;= 0 &amp;&amp; l &lt;= 100 &amp;&amp;
<a name="line423"></a>        a &gt;= 0 &amp;&amp; a &lt;= 1) {
<a name="line424"></a>      return [h, s, l, a];
<a name="line425"></a>    }
<a name="line426"></a>  }
<a name="line427"></a>  return [];
<a name="line428"></a>};
<a name="line429"></a>
<a name="line430"></a>
<a name="line431"></a>/**
<a name="line432"></a> * Takes an array of [r, g, b, a] and converts it into a string appropriate for
<a name="line433"></a> * CSS styles.
<a name="line434"></a> * @param {Array.&lt;number&gt;} rgba [r, g, b, a] with r, g, b in [0, 255] and a
<a name="line435"></a> *     in [0, 1].
<a name="line436"></a> * @return {string} string of the form &#39;rgba(r,g,b,a)&#39;.
<a name="line437"></a> * @private
<a name="line438"></a> */
<a name="line439"></a>goog.color.alpha.rgbaStyle_ = function(rgba) {
<a name="line440"></a>  return &#39;rgba(&#39; + rgba.join(&#39;,&#39;) + &#39;)&#39;;
<a name="line441"></a>};
<a name="line442"></a>
<a name="line443"></a>
<a name="line444"></a>/**
<a name="line445"></a> * Converts from h,s,v,a values to a hex string
<a name="line446"></a> * @param {number} h Hue, in [0, 1].
<a name="line447"></a> * @param {number} s Saturation, in [0, 1].
<a name="line448"></a> * @param {number} v Value, in [0, 255].
<a name="line449"></a> * @param {number} a Alpha, in [0, 1].
<a name="line450"></a> * @return {string} hex representation of the color.
<a name="line451"></a> */
<a name="line452"></a>goog.color.alpha.hsvaToHex = function(h, s, v, a) {
<a name="line453"></a>  var alpha = Math.floor(a * 255);
<a name="line454"></a>  return goog.color.hsvArrayToHex([h, s, v]) +
<a name="line455"></a>         goog.color.prependZeroIfNecessaryHelper(alpha.toString(16));
<a name="line456"></a>};
<a name="line457"></a>
<a name="line458"></a>
<a name="line459"></a>/**
<a name="line460"></a> * Converts from an HSVA array to a hex string
<a name="line461"></a> * @param {Array} hsva Array of [h, s, v, a] in
<a name="line462"></a> *     [[0, 1], [0, 1], [0, 255], [0, 1]].
<a name="line463"></a> * @return {string} hex representation of the color.
<a name="line464"></a> */
<a name="line465"></a>goog.color.alpha.hsvaArrayToHex = function(hsva) {
<a name="line466"></a>  return goog.color.alpha.hsvaToHex(hsva[0], hsva[1], hsva[2], hsva[3]);
<a name="line467"></a>};
</pre>


</body>
</html>
